﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace BasicTCPClientServer
{
    public partial class Client : Form
    {
        public Client()
        {
            InitializeComponent();
            
        }

        private void Send_Click(object sender, EventArgs e)
        {
            TcpClient client = new TcpClient();
            MessageBox.Show("Init connection.");
            client.Connect(textBoxIPAddress.Text, 7979);
            MessageBox.Show("Connected.");

            Stream stm = client.GetStream();
            ASCIIEncoding asci = new ASCIIEncoding();
            byte[] b = asci.GetBytes(richTextBox.Text);
            MessageBox.Show("Transmitting.");

            stm.Write(b, 0, b.Length);

            byte[] br = new byte[100];
            string r = "";
            int k = stm.Read(br, 0, 100);
            for (int i = 0; i < k; i++)
                r = r + Convert.ToChar(br[i]);

            MessageBox.Show("Received message:" + r);
            
            client.Close();
        }
    }
}
